package com.etc.cloud.controller;

import com.etc.cloud.entityes.CommonResult;
import com.etc.cloud.entityes.Payment;
import com.etc.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Author kalista
 * @Description 支付控制器
 * @Date 2020/8/16  14:48
 **/
@Slf4j
@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    String serverPort;

    /**
     * 支付添加
     * @param payment
     * @return
     */
    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        log.info("************** 插入结果: " + i);
        if (i > 0){
            return new CommonResult(200,"插入数据库 成功 " + serverPort,i );
        }else {
            return new CommonResult(444,"插入数据库 失败 ",null );
        }
    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        if (payment != null){
            return new CommonResult<>(200,"查询成功 serverport:"+serverPort,payment);
        }else {
            return new CommonResult<>(444,"没有对应得记录 查询ID",null);
        }
    }

    @GetMapping("/payment/lb")
    public String getServerPort(){
        return serverPort;
    }


    @GetMapping("/payment/feigin/timeout")
    public String paymentFeginFTimeOut(){

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        return serverPort;
    }

}
